package com.fsi.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;

public class ApplicationEntryPoint implements EntryPoint {

	private ApplicationGUI gui;
	private ApplicationServiceDelegate delegate;
	private final ClickHandler menuButtonHandler = getMenuButtonHandler();

	@Override
	public void onModuleLoad() {
		delegate = new ApplicationServiceDelegate();

		gui = new ApplicationGUI();
		gui.service = delegate;
		delegate.gui = gui;
		gui.initGui();

		wireEvents();
	}

	private void wireEvents() {
		gui.homeButton.addClickHandler(menuButtonHandler);
		// gui.personalButton.addClickHandler(menuButtonHandler);
		gui.editPersonalBtn.addClickHandler(menuButtonHandler);
		gui.testButton.addClickHandler(menuButtonHandler);
		gui.getPhotoButton.addClickHandler(menuButtonHandler);
		gui.optionsButton.addClickHandler(menuButtonHandler);
		gui.loginButton.addClickHandler(menuButtonHandler);
		gui.showPhotosButton.addClickHandler(menuButtonHandler);
	}

	private ClickHandler getMenuButtonHandler() {
		return new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (!gui.loginInfo.isLoggedIn()) {
					Window.alert("Not logged in");
				} else {

					if (event.getSource().equals(gui.testButton)) {
						gui.showTest(true);
					} else if (event.getSource().equals(gui.homeButton)) {
						gui.showHome(true);
					} else if (event.getSource().equals(gui.personalButton)) {
						gui.showPersonal(true);
					} else if (event.getSource().equals(gui.editPersonalBtn)) {
						gui.showEditCustomer(true);
					} else if (event.getSource().equals(gui.optionsButton)) {
						gui.showOptions(true);
					} else if (event.getSource().equals(gui.loginButton)) {
						gui.showLoginOrLogout();
					} else if (event.getSource().equals(gui.getPhotoButton)) {
						gui.showGetPhoto(true);
					} else if (event.getSource().equals(gui.showPhotosButton)) {
						gui.showPhotos(true);
					}
				}
			}
		};

	}
}
